package com.cskaoyan.service.market;

import com.cskaoyan.bean.market.Category;
import com.cskaoyan.bean.market.CategoryExample;
import com.cskaoyan.bo.market.CategoryCreateBo;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.utils.Converter;
import com.cskaoyan.utils.PojoUtils;
import com.cskaoyan.vo.market.CategoryCreateVo;
import com.cskaoyan.vo.market.CategoryL1Vo;
import com.cskaoyan.vo.market.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @program: mall
 * @description:
 * @author: Mr.Mengmeng
 * @create: 2021-04-09 23:01
 **/

// 商品类目
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<CategoryVo> categoryList() {
        // 查询获取一级目录数据
        CategoryExample first = new CategoryExample();
        first.createCriteria().andDeletedEqualTo(false).andPidEqualTo(0).andLevelEqualTo("L1");
        List<Category> firstCategories = categoryMapper.selectByExample(first);
        // 查询获取二级目录数据
        CategoryExample second = new CategoryExample();
        second.createCriteria().andDeletedEqualTo(false).andPidNotEqualTo(0).andLevelEqualTo("L2");
        List<Category> secondCategories = categoryMapper.selectByExample(second);

        ArrayList<CategoryVo> categoryVoList = new ArrayList<>();

        for (Category firstC : firstCategories) {
            // 封装一级目录数据
            CategoryVo categoryVo = new CategoryVo(firstC.getId(), firstC.getName(), firstC.getKeywords(), firstC.getDesc(),
                    firstC.getIconUrl(), firstC.getPicUrl(), firstC.getLevel(), null);

            ArrayList<CategoryVo.ChildrenBean> childrenBeans = new ArrayList<>();
            for (Category secondC : secondCategories) {

                if (secondC.getPid().equals(firstC.getId())) {
                    // 封装二级目录数据
                    CategoryVo.ChildrenBean childrenBean = new CategoryVo.ChildrenBean(secondC.getId(), secondC.getName(),
                            secondC.getKeywords(), secondC.getDesc(), secondC.getIconUrl(), secondC.getPicUrl(),
                            secondC.getLevel());
                    childrenBeans.add(childrenBean);
                }
            }
            categoryVo.setChildren(childrenBeans);

            categoryVoList.add(categoryVo);
        }

        return categoryVoList;
    }

    @Override
    public int categoryUpdate(CategoryVo bo) {
        // 如果要修改的是一级目录
        // 如果要把一级目录修改成二级目录，那他下面的子目录需要跟着改变主人吗？


        // 一级目录可以更改，但是其子目录如果全部转为别人的名下？
        // 那么，这里应该怎么设置。
        // 转为别人的名下，也是可通过设置目录改回来的，如果还跟随原来的目录

        // 一级目录的操作：
        //    改成二级目录,不能改为自己目录下。必须找一个已有的一级目录。

        // 二级目录操作：
        //    变更为一级目录，
        //    变更为其他一级目录下的二级目录。


        // TODO： 这里先默认没有关键字，没理清楚关键字跳转的逻辑

        // 类型转换
        Category category = new Category();
        Converter converter = PojoUtils.converter();
        converter.converter(bo, category);

        // 一级目录的操作
        if (bo.getChildren() != null) {
            // 判断是否要变更目录级别
            if (bo.getPid() != null) {
                // 不能更改为自己目录下
                if (bo.getPid().equals(bo.getId())) {
                    return 0;
                }
                // 更改为其他目录名下，子目录跟着一起过去，显示操作,()
                CategoryExample categoryExample = new CategoryExample();
                CategoryExample.Criteria criteria = categoryExample.createCriteria();
                criteria.andDeletedEqualTo(false)
                        .andLevelEqualTo(bo.getLevel())
                        .andPidEqualTo(bo.getPid());
                List<Category> categories = categoryMapper.selectByExample(categoryExample);
                Byte sortOrder = categories.get(0).getSortOrder();
                for (int i = 0; i < categories.size(); i++) {
                    // sortOrder 排序的数字
                    if (sortOrder < categories.get(i).getSortOrder()) {
                        sortOrder = categories.get(i).getSortOrder();
                    }
                }
                sortOrder = sortOrder++;
                // 修改
                category.setUpdateTime(new Date());
                category.setSortOrder(sortOrder);
                categoryMapper.updateByPrimaryKeySelective(category);
                // 修改子目录
                for (CategoryVo.ChildrenBean child : bo.getChildren()) {
                    Category category1 = new Category();
                    Converter converter1 = PojoUtils.converter();
                    converter1.converter(child, category);

                    sortOrder = sortOrder++;

                    category1.setSortOrder(sortOrder);
                    category1.setUpdateTime(new Date());
                    categoryMapper.updateByPrimaryKeySelective(category1);
                }
            }
            // 不修改目录级别，只做其他参数操作
            if (bo.getPid() == null) {
                category.setUpdateTime(new Date());
                int i = categoryMapper.updateByPrimaryKeySelective(category);
                return i;
            }
        }
        // 二级目录的操作
        if(bo.getPid()==null){
            return 401;
        }
        // 二级目录参数的操作
        category.setUpdateTime(new Date());
        int i = categoryMapper.updateByPrimaryKeySelective(category);
        return i;
    }

    @Override
    public void deleteCategory(Category category) {

        category.setDeleted(true);
        category.setUpdateTime(new Date());
        categoryMapper.updateByPrimaryKeySelective(category);

    }

    @Override
    public List<CategoryL1Vo> categoryL1() {
        // 获取所有pid为0的数据==也就是等级为L1
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andPidEqualTo(0);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);

        // 类型转换
        ArrayList<CategoryL1Vo> list = new ArrayList<>();
        for (Category category : categories) {
            CategoryL1Vo categoryL1Vo = new CategoryL1Vo(category.getId(), category.getName());
            list.add(categoryL1Vo);
        }

        return list;
    }

    @Override
    public CategoryCreateVo createCategory(CategoryCreateBo categoryCreateBo) {
        // 类型转换接收参数
        Category category = new Category();
        Converter converter = PojoUtils.converter();
        converter.converter(categoryCreateBo, category);
        // 添加数据操作
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        category.setDeleted(false);
        categoryMapper.insertSelective(category);
        // 转换，返回参数
        CategoryCreateVo vo = new CategoryCreateVo();
        Converter converter1 = PojoUtils.converter();
        converter1.converter(category, vo);

        return vo;
    }

    @Override
    public Map<String, Object> wxCurrentQuery(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        Category category = categoryMapper.selectByPrimaryKey(id);
        map.put("currentCategory", category);
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria()
                .andPidEqualTo(category.getId())
                .andLevelEqualTo("L2")
                .andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        map.put("currentSubCategory", categories);
        return map;
    }

    @Override
    public Map<String, Object> wxIndexQuery() {
        HashMap<String, Object> map = new HashMap<>();
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andDeletedEqualTo(false).andLevelEqualTo("L1");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        Category category = categories.get(0);
        map.put("categoryList", categories);
        map.put("currentCategory", category);
        Integer pid = category.getId();
        CategoryExample categoryExample2 = new CategoryExample();
        categoryExample2.createCriteria().andPidEqualTo(pid).andLevelEqualTo("L2");
        List<Category> listL2 = categoryMapper.selectByExample(categoryExample2);
        map.put("currentSubCategory", listL2);
        return map;
    }


}
